<template>
	<view class="cardBox" v-if="loadings" style="padding: 0;">
		<view
			style="text-align: center; background: #3e72ff;color: #fff;padding: 2% 5%;width: 60%;margin: 0 auto;border-radius: 0 0 50% 50%;background: linear-gradient(to right, #3e72ff, #86cfff);">
			{{$t("第1步 合作协议")}}
		</view>
		<view style="padding: 5px 10px;">
			<view class="flex" style="margin-top: 50rpx;">
				<view class="biao" style="width: 100%;">
					<span class="biao_span1">1</span>
					<text class="cardTitle">{{text.contract}} </text>
					
					<block v-if="plat=='app'">
						<view class="order_info_btn" style="float: right;" :class="plat=='app'?'touming':''"
							v-if="order.orderInfo.confirm_contract==0 || order.orderJoin.confirm_contract==0">
							<view v-if="plat=='app'"><upload @change="upload" :text="text.upload"></upload></view>
						</view>
					</block>
					<block v-if="plat!='app'">
						<view class="order_info_btn orange" style="float: right;" :class="plat=='app'?'touming':''"
							v-if="order.orderInfo.confirm_contract==0 || order.orderJoin.confirm_contract==0">
							<view v-if="plat!='app'">
								<formFiles3 :item="{}" :showDataList="[]" :color="'white'" :text="text.upload" @change="upload"></formFiles3>
							</view>
						</view>
					</block>
					
					
					
					<view style="float: right;font-size: 10px;line-height: 24px;margin-right: 10px;color: red;"
						v-if="scupload">{{$t("已上传")}}</view>
					<view class="order_info_btn"
						style="background: #38de6a;font-size: 12px;color: #ffffff;text-align: center;border-radius: 5px;padding: 5px 10px;margin-top: 2px;float: right;"
						v-if="yqr">{{$t("已确认")}}</view>
					<view style="clear: both;"></view>
				</view>
			</view>
			<view style="width: 100%;margin:0rpx 20rpx;padding: 20rpx 0rpx;">

				<block v-if="wait_upload_flag==1">

					<formFiles3 :item="{}" :text="text.wait_upload"
						:showDataList="order.orderInfo.transaction_contract||[]" v-if="loading" @change="upload"
						:postht="1"></formFiles3>
				</block>

				<block v-if="wait_upload_flag==0">

					{{text.wait_upload}}
				</block>
				<block v-if="order.orderInfo.transaction_contract[0].url.length!=0">
					<view class="buy_files" style="width: 95%;position: relative;z-index: 33;"
						v-if="order.orderInfo.transaction_contract[0].url">
						<view style="padding: 5px 10px;" class="downLoad" @click="qrht()">
							{{ order.orderInfo.transaction_contract[0].url.substring(order.orderInfo.transaction_contract[0].url.length, order.orderInfo.transaction_contract[0].url.length - 20) }}
						</view>
					</view>
				</block>
				<block v-else>
					<view class="buy_files" style="width: 95%;" v-if="contract_upload">
						<view style="padding: 5px 10px;" class="downLoad">
							{{ contract_upload.substring(contract_upload.length, contract_upload.length - 20) }}
						</view>
					</view>
				</block>
			</view>
			<view style="float: left;color: red;margin-bottom: 5%;font-size: 10px;margin-left: 10px;" v-if="scupload">
				{{$t("待对方确认")}}
			</view>
			<view style="float: left;color: #38de6a;margin-bottom: 5%;font-size: 10px;margin-left: 10px;" v-if="yqr">
				{{$t("对方已确认")}}
			</view>
			<view style="float: right;color: red;margin-bottom: 5%;font-size: 12px;" v-if="scbtn" @click="qrht()">
				{{$t("待确认")}}
			</view>
			<view style="height: 10px;"></view>
		</view>

		

		<!--货物清单-->
		<filelist @on-oversize="tijiao" v-if="bcz||hzxy==1" :order2="order2"></filelist>

		<!--委托售后及免责协议-->
		<afterSalesService v-if="bcz||hzxy==1" :order="order2"></afterSalesService>

		<!--验货及赔付标准协议-->
		<inspection v-if="(bcz&&order2.orderInfo.release_info.is_margin==2)||(hzxy==1&&order2.orderInfo.release_info.is_margin==2)" :order2="order2"></inspection>
		
		<view v-if="order2.orderInfo.status==3" class="modal-background2"></view>
		
	</view>
</template>

<script>
	import {
		HTTP_REQUEST_URL,
		HTTP_IMG_UTL,
		IMAGE_URL
	} from "@/config/app";
	import formFiles3 from '../formFiles3.vue'
	import upload from "@/components/buildForm/upload.vue";
	import inspection from "../detail/inspection.vue"
	import afterSalesService from "../detail/afterSalesService.vue"
	import filelist from "../detail/filelist.vue"
	export default {
		components: {
			formFiles3,upload,
			inspection,
			afterSalesService,
			filelist
		},
		props: {
			order2: {
				type: [Array, Object, Number, String],
				default: {}
			},
			orderinfo2: {
				type: [Array, Object, Number, String],
				default: {}
			},
			orderjoin2: {
				type: [Array, Object, Number, String],
				default: {}
			},
			hzxy: {
				type: [Array, Object, Number, String],
				default:""
			}
		},

		data() {
			return {
				//远程域名
				HTTP_IMG_UTL,
				HTTP_REQUEST_URL,
				IMAGE_URL,
				url: IMAGE_URL,
				plat:uni.getStorageSync('plat'),

				//语言
				lang: this.$i18n.locale,
				//文字
				text: {
					contract: this.$t('交易合同'),
					upload: this.$t('上传'),
					wait_upload: this.$t('暂未上传'),
					wait_confirm: this.$t('待对方确认'),
					other_confirm: this.$t('对方已确认'),
					is_confirm: this.$t('您已确认'),
					is_dconfirm: this.$t('待确认'),
					is_reject: this.$t('已拒绝')
				},
				loading: false,
				loadings: false,

				showDataList: [],
				order: {
					orderInfo: {
						transaction_contract: [{
							url: ''
						}]
					}
				},
				orderJoin1: 0,
				orderJoin2: 0,
				wait_upload_flag: 0,
				contract_upload: '',
				bcz: false,
				scbtn: false,
				scupload: false,
				yqr: false,
			}
		},
		mounted() {
			this.order = this.order2
			// console.log(this.order)
			if (this.order.role == 1 || this.order.role == 2) {
				this.orderJoin1 = this.order.orderInfo.confirm_contract
				this.orderJoin2 = this.order.orderJoin.confirm_contract
			}
			if (this.order.orderInfo.transaction_contract.length != 0) {
				if (this.order.orderInfo.transaction_contract[0]) {
					if (this.order.orderInfo.transaction_contract[0].url != '') {
						this.wait_upload_flag = 1
						if ((this.order.orderInfo.transaction_contract == null) || (this.order.orderInfo
								.transaction_contract.length == 0)) {
							this.order.orderInfo.transaction_contract = [{
								url: ''
							}]
						}
						this.contract_upload = this.order.orderInfo.transaction_contract[0].url
					}
				}
			}
			if ((this.order.orderInfo.transaction_contract == null) || (this.order.orderInfo.transaction_contract.length ==
					0)) {
				this.order.orderInfo.transaction_contract = [{
					url: ''
				}]
			}
			if (this.order.orderInfo.confirm_contract == 1 && this.order.role == 1 && this.order.orderJoin
				.confirm_contract == 0) {
				this.text.upload = this.$t("重新上传");
			} else if (this.order.orderJoin.confirm_contract == 1 && this.order.role == 2 && this.order.orderInfo
				.confirm_contract == 0) {
				this.text.upload = this.$t("重新上传");
			}
			this.jinxingzhong()
			this.sc_btn()
			this.sc_upload()
			this.yiqueren()
			
			this.loadings = true
		},

		onShow() {
			if (!this.plat) {
				uni.getSystemInfo({
					success: function(res) {
						if (res.uniPlatform === 'app') {
							this.plat = 'app'
						} else if (res.uniPlatform === 'web') {
							this.plat = 'web'
						} else if (res.uniPlatform === 'mp-weixin') {
							this.plat = 'mp-weixin'
						}
						uni.setStorage({
							key: 'plat',
							data: this.plat
						})
						this.plat = uni.getStorageSync('plat')
					}
				})
			}
		},
		methods: {
			yiqueren() {
				if (this.order.orderInfo.confirm_contract == 1 && this.order.orderJoin.confirm_contract == 1) {
					this.yqr = true;
					this.scupload = false;
					this.scbtn = false;
				}
			},
			qrht() {
				uni.navigateTo({
					url: "/pages/textbook/qrht?order_id=" + this.order.orderInfo.id + "&join_order_id=" +
						this.order.orderInfo.join_order_id
				})
			},
			sc_btn() {
				if ((this.order.role == 1 && this.order.orderInfo.confirm_contract == 0 && this.order.orderJoin
						.confirm_contract == 1) ||
					(this.order.role == 2 && this.order.orderJoin.confirm_contract == 0 && this.order.orderInfo
						.confirm_contract == 1)) {
					this.scbtn = true
				}
			},
			sc_upload() {
				if (this.order.orderInfo.confirm_contract == 0 && this.order.orderJoin.confirm_contract == 0) {
					this.scupload = false;
				} else if (this.order.role == 1 && this.order.orderInfo.confirm_contract == 0 && this.order.orderJoin
					.confirm_contract == 1) {
					this.scupload = false;
				} else if (this.order.role == 1 && this.order.orderInfo.confirm_contract == 1 && this.order.orderJoin
					.confirm_contract == 0) {
					this.scupload = true;
				} else if (this.order.role == 2 && this.order.orderInfo.confirm_contract == 0 && this.order.orderJoin
					.confirm_contract == 1) {
					this.scupload = true;
				} else if (this.order.role == 2 && this.order.orderInfo.confirm_contract == 1 && this.order.orderJoin
					.confirm_contract == 0) {
					this.scupload = false;
				}
			},
			jinxingzhong() {
				if (this.order.orderInfo.release_info.is_margin == 2) {
					if (this.order.orderInfo.is_compensation != 1 || this.order.orderJoin.is_compensation != 1 || this
						.order.orderInfo.is_agreement != 1 || this.order.orderJoin.is_agreement != 1 || this.order
						.orderInfo.confirm_contract != 1 || this.order.orderJoin.confirm_contract != 1 || (this.order
							.orderInfo.goods_list && this.order.orderJoin.is_goodslist == 0) ||
						(this.order.orderJoin.goods_list && this.order.orderInfo.is_goodslist == 0) || (!this.order
							.orderInfo.goods_list && !this.order.orderJoin.goods_list)) {
						this.bcz = true;
					}
				} else {
					if (this.order.orderInfo.is_agreement != 1 || this.order.orderJoin.is_agreement != 1 || this.order
						.orderInfo.confirm_contract != 1 || this.order.orderJoin.confirm_contract != 1 || (this.order
							.orderInfo.goods_list && this.order.orderJoin.is_goodslist == 0) ||
						(this.order.orderJoin.goods_list && this.order.orderInfo.is_goodslist == 0) || (!this.order
							.orderInfo.goods_list && !this.order.orderJoin.goods_list)) {
						this.bcz = true;
					}
				}
			},
			//下载文件
			downLoadFile(url) {
				const that = this
				uni.showLoading({
					title: "download...",
					mask: true
				})
				console.log('下载链接', url)
				var downloadTask = uni.downloadFile({
					url: url,
					success(res) {
						uni.hideLoading()
						console.log('tempFilePath', res.tempFilePath)
						const tempFilePath = res.tempFilePath
						uni.openDocument({
							filePath: tempFilePath,
							showMenu: true,
							success: function(file) {
								console.log('file-success', file);
							}
						});
					},
				})
				downloadTask.onProgressUpdate(({
					progress,
					totalBytesWritten,
					totalBytesExpectedToWrite
				}) => {
					if (progress === 100) {
						downloadTask.offProgressUpdate();
					}
				})
			},
			upload(e, data) {
				if(this.plat!='app'){
					this.contract_upload = data[0].url
					this.showDataList = data
				}else{
					this.contract_upload = data.url
					this.showDataList = [data]
				}
				var that = this;
				if(that.contract_upload==''){
					that.$u.toast(this.$t('文件格式不正确'))
					return;
				}
				this.loading = false
				if (e && data) {
					this.$u.api.order.upTransactionContract({
						transaction_contract: this.contract_upload,
						order_id: this.order.orderInfo.id,
						join_order_id: this.order.orderInfo.join_order_id,
						role: this.order.role,
					}).then(res => {
						that.$u.toast(this.$t('操作成功'))
						that.$emit('on-oversize', 1);
					})
				}
			
				this.loading = true
			},
			confirmContract(role) {
				uni.showModal({
					title: this.$t('确认合同'),
					content: this.$t("是否确认合同"),
					confirmText: this.$t('是'), // 修改确认按钮文字
					cancelText: this.$t('否'),
					success: (res) => {
						if (res.confirm) {
							const that = this
							that.$u.api.order.confirmContract({
								role: role,
								order_id: this.order.orderInfo.id,
								join_order_id: this.order.orderInfo.join_order_id,
							}).then(res => {
								this.$u.toast(this.$t('操作成功'))
								setTimeout(() => {
									// 页面重载
									const pages = getCurrentPages()
									// 声明一个pages使用getCurrentPages方法
									const curPage = pages[pages.length - 1]
									// 声明一个当前页面
									curPage.onLoad(curPage.options) // 传入参数
									curPage.onShow()
									curPage.onReady()
									// 执行刷新
								}, 1000)
							})
							// 用户点击了确定，执行相应的逻辑
						} else if (res.cancel) {
							// 用户点击了取消，执行相应的逻辑
						}
					}
				})
			},
			tijiao(){
				this.$emit('on-oversize', 1);
			},

		}
	}
</script>

<style lang="scss" scoped>
	@import "@/common/orderDetail.scss";
	.touming{ background: transparent !important; }
	.modal-background2 {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.01);
		z-index: 9999;
	}
</style>